package com.meila.cms.services.impl;

import java.util.Date;
import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;

import com.github.miemiedev.mybatis.paginator.domain.PageBounds;
import com.meila.cms.dao.DictMapper;
import com.meila.cms.dao.UserMapper;
import com.meila.cms.dao.UserRoleMapper;
import com.meila.cms.entity.AjaxObj;
import com.meila.cms.entity.Role;
import com.meila.cms.entity.User;
import com.meila.cms.entity.UserRole;
import com.meila.cms.model.CmsException;
import com.meila.cms.model.UserModel;
import com.meila.cms.services.IUserService;
import com.meila.cms.util.SecurityUtil;
import com.meila.cms.util.page.PageBoundsUtil;
import com.meila.cms.util.page.Pager;

@Service
public class UserService extends BaseService implements IUserService {


	@Override
	public void add(User user, Integer[] rids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public UserModel load(int id) {
		// TODO Auto-generated method stub
	    UserModel userModel=(UserModel) userMapper.selectByPrimaryKey(id);
		
		return userModel;
	}

	@Override
	public List<Integer> listUserRoleIds(int id) {
		// TODO Auto-generated method stub
		return null;
	}
 

	@Override
	public void delete(int id) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void update(User user, Integer[] rids) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public AjaxObj update(UserModel userModel) {
		AjaxObj  ajaxObj=new AjaxObj();
		try {
			userModel.setUpdateTime(new Date());
			userMapper.updateByPrimaryKeySelective(userModel);
			ajaxObj.setMsg("操作成功");
		} catch (Exception e) {
			// TODO: handle exception
			ajaxObj.setResult(false);
			ajaxObj.setMsg("操作失败");
			e.printStackTrace();
		}
		
		
		return ajaxObj;
		
	}

	@Override
	public void updatePwd(int uid, String oldPwd, String newPwd) {
		// TODO Auto-generated method stub
		
	}

 
	@Override
	public Pager<UserModel> findUser(UserModel userModel) {
 
		
		//封装PageBounds对象
		PageBounds pageBounds = PageBoundsUtil.PageBoundsOrderExtend("createTime.desc");
		
		//获取用户总数
		int count =	userMapper.findUserCount(userModel,pageBounds);
		
		//获取用户分页列表集合信息
		List<UserModel> list = userMapper.findUser(userModel,pageBounds);
		//封装用户分页的Pager对象
		Pager<UserModel> pages = new Pager<UserModel>(count,list);
		return pages;
	}

	@Override
	public List<Role> listRolesByUid(int id) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<UserRole> listUserRoles(int id) {
		// TODO Auto-generated method stub
		
		return userRoleMapper.listUserRoles(id);
 
	}

	@Override
	public List<UserRole> listRoleUsers(int rid) {
		// TODO Auto-generated method stub
		return userRoleMapper.listRoleUsers(rid);
	}

	@Override
	public AjaxObj login(String phoneNo, String password) {
		
		UserModel userModel = userMapper.loadByPhone(phoneNo);
		
		AjaxObj ajaxObj=new AjaxObj();
		ajaxObj.setResult(true);
		
		
		String errmsg=null;
		try {
			if(userModel==null){
				 errmsg="用户未注册";
				 throw new CmsException(errmsg);
			}else if (0==userModel.getStatus()) {
				 errmsg="用户已禁用";
				 throw new CmsException(errmsg);
			}
	        System.out.println(SecurityUtil.md5(password));
			if(!SecurityUtil.md5(password).equals(userModel.getPassword())) {
				errmsg="用户名或者密码不正确";
				throw new CmsException(errmsg);
			}
		} catch (Exception e) {
			ajaxObj.setMsg(errmsg);
			ajaxObj.setResult(false);
			e.printStackTrace();
		}
		
		ajaxObj.setObj(userModel);
 
		return ajaxObj;
	}

	@Override
	public List<User> listUsersByRid(int id) {
		// TODO Auto-generated method stub
		return null;
	}
 
	 
}
